#!/usr/bin/env python3
"""
deepin-graphics-driver-manager MCP Server

面向 deepin-graphics-driver-manager 软件的 MCP Server，提供命令行操作封装。

MCP tools 列表：
1. deepin_graphics_driver_manager_service_status
   - 用途：检查 driver-installer.service 服务的状态
   - 参数：无
   - 返回：包含服务状态信息的统一 JSON 结构

2. deepin_graphics_driver_manager_service_restart
   - 用途：重启 driver-installer.service 服务
   - 参数：无
   - 返回：包含重启操作结果的统一 JSON 结构
"""

from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json

mcp = FastMCP("Deepin Graphics Driver Manager MCP Server")

@mcp.tool()
def deepin_graphics_driver_manager_service_status() -> dict:
    """
    检查 driver-installer.service 服务的状态
    
    返回 JSON 结构：
    {
        "success": bool,      # 命令执行是否成功
        "command": str,       # 执行的完整命令
        "exit_code": int,     # 命令退出码
        "stdout": str,        # 标准输出内容
        "stderr": str         # 标准错误内容
    }
    """
    command = ["systemctl", "status", "driver-installer.service"]
    
    try:
        result = subprocess.run(
            command,
            capture_output=True,
            text=True,
            timeout=30
        )
        
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Command execution failed: {str(e)}"
        }

@mcp.tool()
def deepin_graphics_driver_manager_service_restart() -> dict:
    """
    重启 driver-installer.service 服务
    
    返回 JSON 结构：
    {
        "success": bool,      # 命令执行是否成功
        "command": str,       # 执行的完整命令
        "exit_code": int,     # 命令退出码
        "stdout": str,        # 标准输出内容
        "stderr": str         # 标准错误内容
    }
    """
    command = ["systemctl", "restart", "driver-installer.service"]
    
    try:
        result = subprocess.run(
            command,
            capture_output=True,
            text=True,
            timeout=30
        )
        
        return {
            "success": result.returncode == 0,
            "command": " ".join(command),
            "exit_code": result.returncode,
            "stdout": result.stdout,
            "stderr": result.stderr
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": " ".join(command),
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Command execution failed: {str(e)}"
        }

if __name__ == "__main__":
    mcp.run()